package org.opens.javaskill.design;

public class VisitorPattern {

    public static void main(String[] args) {
        ComputerPartDisplayVisitor visitor = new ComputerPartDisplayVisitor();
        Computer computer = new Computer();
        computer.accept(visitor);
    }

}

interface ComputerPart {

    public void accept(ComputerPartVisitor visitor);

}

interface ComputerPartVisitor {

    public void visit(Computer computer);

    public void visit(KeyBoard keyBoard);

    public void visit(Memory memory);

    public void visit(Mouse mouse);

}

class KeyBoard implements ComputerPart {

    @Override
    public void accept(ComputerPartVisitor visitor) {
        visitor.visit(this);
    }

}

class Memory implements ComputerPart {

    @Override
    public void accept(ComputerPartVisitor visitor) {
        visitor.visit(this);
    }

}

class Mouse implements ComputerPart {

    @Override
    public void accept(ComputerPartVisitor visitor) {
        visitor.visit(this);
    }

}

class Computer implements ComputerPart {

    private ComputerPart[] parts;

    public Computer() {
        this.parts = new ComputerPart[]{
                new KeyBoard(),
                new Memory(),
                new Mouse()
        };
    }

    @Override
    public void accept(ComputerPartVisitor visitor) {
        for (ComputerPart part : parts) {
            part.accept(visitor);
        }
        visitor.visit(this);
    }

}

class ComputerPartDisplayVisitor implements ComputerPartVisitor {

    @Override
    public void visit(Computer computer) {
        System.out.println("computer 被访问");
    }

    @Override
    public void visit(KeyBoard keyBoard) {
        System.out.println("keyBoard 被访问");
    }

    @Override
    public void visit(Memory memory) {
        System.out.println("memory 被访问");
    }

    @Override
    public void visit(Mouse mouse) {
        System.out.println("mouse 被访问");
    }

}